package com.wilson.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.wilson.commonutils.R;
import com.wilson.servicebase.config.exceptionHandler.GuliException;
import com.wilson.vod.Service.VodService;
import com.wilson.vod.utils.ConstantPropertiesUtils;
import com.wilson.vod.utils.InitVodObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("eduvod/vod")
@CrossOrigin//跨域
@Api(description="阿里云视频点播微服务")
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo( @ApiParam(name = "file", value = "文件", required = true)
        @RequestParam("file") MultipartFile file){
        //返回上传视频的id
        String videoId = vodService.uploadVideoAliyun(file);
        return R.ok().message("视频上传成功").data("videoId",videoId);
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("removeAliyunVideo/{videoId}")
    public R removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required = true)
        @PathVariable String videoId){
        vodService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }

    //根据id删除多个阿里云视频
    //参数为多个视频id
    @DeleteMapping("/removeBatch")
    public R removeBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeBatchVideo(videoIdList);
        return R.ok();
    }

    //根据视频id获取视频凭证
//    @GetMapping("/getPlayAuth/{id}")
//    public R getVideoPlayAuth(@PathVariable String id){
//        try {
//            String playAuth = vodService.getVideoPlayAuth(id);
//            return R.ok().data("PlayAuth",playAuth);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new GuliException(20001,"获取视频凭证失败");
//        }
//    }

    //根据视频id获取视频凭证
    @GetMapping("/getPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        //获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;

        //初始化
        DefaultAcsClient client = InitVodObject.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //设置播放凭证的有效期200s
        request.setAuthInfoTimeout(200L);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }


}
